package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;

import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.tools.DensityUtils;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.ParcelBean;

import droid.frame.view.xlist.FooterListener;
import droid.frame.view.xlist.SListView;

/**
 * 包裹列表-已到店，已取走，已超期的的包裹列表 <br>
 * Created by joker on 2017/5/25.
 */
public abstract class BaseParcelFragment extends BaseFragment {

    protected SListView mListView;
    protected ParcelAdapter mAdapter;
    protected SwipeRefreshLayout mRefreshLayout;

    protected ReqResult<ParcelBean> result;

    protected int parcelType = 0;//1已到店 2已取走 3已超期
    protected String expressCompany;// 查询条件--快递公司
    protected String date;// 日期

    /**
     * 记录点击的item
     */
    protected ParcelBean clickItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        super.findViewById();
        mListView = (SListView) findViewById(R.id.listview);
        mListView.setDividerHeight(DensityUtils.dip2px(getActivityContext(), 11));
        mListView.setDivider(null);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ParcelBean parcel = (ParcelBean) parent.getItemAtPosition(position);
                if (parcel == null) {
                    return;// 头部
                }
                clickItem = parcel;
                Intent intent = new Intent();
                intent.setClass(getActivityContext(), ParcelDetailsActivity.class);
                intent.putExtra("item", parcel);
                startActivityForResult(intent, 105);
            }
        });

        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        // mEmptyLayout = (LinearLayout) findViewById(R.id.empty_receive_layout);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().takeParcelQueryList(parcelType, 1, expressCompany, date);
            }
        });

        mListView.setFooterListener(new FooterListener() {
            @Override
            public void onFooterVisible() {
                if (result != null && result.getPageNum() < result.getTotalPage()) {
                    AppHttp.getInstance().takeParcelQueryList(parcelType, result.getPageNum() + 1, expressCompany, date);
                }
            }
        });
    }

    protected ReqResult<ParcelBean> handleMessage(Object msgObj) {
        cancelLoadingDialog();
        onRefreshComplete();
        try {
            result = JSON.parseList(msgObj, ParcelBean.class);// 解析可能出现问题
            getActivityContext().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (checkLoginStatus(result)) {
                        //
                        if (mListView.getAdapter() == null) {
                            mListView.setAdapter(mAdapter);
                        }
                        if (result.getPageNum() == 1) {
                            mAdapter.setItems(result.getResultList(), true);
                        } else {
                            mAdapter.appendItems(result.getResultList(), true);
                        }
                        mListView.updateFooter(result.getPageNum(), result.getTotalPage());
                    } else {
                        // 未知原因是否需要显示
                        showToast(result.getMessage());
                    }
                }
            });
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 105) {
            getActivityContext().setResult(Activity.RESULT_OK);
            getActivityContext().finish();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
